<template>
    <xh-pages :loading="pageData.loading">
        <web-view :src="url" @load="hanldLoad" @error="hanldError" />
    </xh-pages>
</template>
    
<script setup lang="ts">
import { useSite } from '@/stores/site';
import { toast } from '@/utils/helpers';

const siteInfo = storeToRefs(useSite()).siteInfo
const pageData = ref({
    loading: true,
    type: '',
    name: '',
})
const url = ref('')

// 加载成功
const hanldLoad = () => {
    pageData.value.loading = false;
}
// 加载错误
const hanldError = () => {
    console.log('123');
    toast('加载失败', {
        success: () => {
            setTimeout(() => {
                uni.navigateBack()
            }, 1500);
        }
    })
}

const initify = () => {
    switch (pageData.value.type) {
        case 'tags':
            url.value = siteInfo.value.tag_url.replace(`{name}`, pageData.value.name)
            break;
        case 'help':
            url.value = siteInfo.value.help_url
            break;
    }
    // #ifdef H5
    pageData.value.loading = false;
    uni.setNavigationBarTitle({
        title: siteInfo.value.web_name
    })
    // #endif
}

onLoad((e) => {
    pageData.value.type = e?.type ?? ''
    pageData.value.name = e?.name ?? ''
    initify();
})
</script>
    
<style lang="scss" scoped></style>
    